/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 20, 2012  12:52:50 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : FiArreceiptreOutbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.FiArreceiptreOutb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class FiArreceiptreOutbServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(FiArreceiptreOutbServices.class);
	

	/**
	 * @param em
	 */
	public FiArreceiptreOutbServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public FiArreceiptreOutb findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "FiArreceiptreOutb.findById"));
	}
	
	public FiArreceiptreOutb Updated(FiArreceiptreOutb value) throws Exception {
		if(value != null)
		{
			return (FiArreceiptreOutb) super.updated(value);
		}else
			return null;
	}
	
	public List<FiArreceiptreOutb> findIsSync(String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiArreceiptreOutb.findIsSync", mapWhereValue));		
	}
	
	public List<FiArreceiptreOutb> findInstanceNo(String company, String project, String receiptDocNo, String instanceNo, String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("receiptDocNo", receiptDocNo);
		mapWhereValue.put("instanceNo", instanceNo);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiArreceiptreOutb.findInstanceNo", mapWhereValue));		
	}
	/**
	 * Without issync
	 * @param instanceNo
	 * @return
	 * @throws Exception
	 */
	public List<FiArreceiptreOutb> findInstanceNo1(String instanceNo) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();		
		mapWhereValue.put("instanceNo", instanceNo);		
		
		return super.ConvertToYoureObject(super.find("FiArreceiptreOutb.findInstanceNo1", mapWhereValue));		
	}
	
	public List<FiArreceiptreOutb> findByDocumentNo(String company, String project, String receiptDocNo, String status, String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("receiptDocNo", receiptDocNo);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiArreceiptreOutb.findByDocumentNo", mapWhereValue));		
	}
	
	public List<FiArreceiptreOutb> findByRefDocumentNo(String company, String project, String refDocumentNo, String status, String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("refDocumentNo", refDocumentNo);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiArreceiptreOutb.findByRefDocumentNo", mapWhereValue));
	}
	
	public List<String> GroupByDocumentNo(String company, String project, String refDocumentNo, String status, String issync) throws Exception {
		try{
			
			if(!CommonService.IsNull(refDocumentNo) && !CommonService.IsNull(company) && !CommonService.IsNull(project) && !CommonService.IsNull(status) )
			{
				StringBuilder _strQuery = new StringBuilder();
				_strQuery.append(" Select RECEIPT_DOC_NO From FI_ARRECEIPTRE_OUTB ");
				_strQuery.append(" Where COMPANY = ").append(Utilitys.strSql(company));
				_strQuery.append(" And PROJECT = ").append(Utilitys.strSql(project));
				_strQuery.append(" and REF_CONTRACT_NO = ").append(Utilitys.strSql(refDocumentNo));
				_strQuery.append(" And STATUS = ").append(Utilitys.strSql(status));
				
				if(!CommonService.IsNull(issync))
					_strQuery.append(" And ISSYNC = ").append(Utilitys.strSql(issync));
				
				_strQuery.append(" Group by RECEIPT_DOC_NO ");
				_strQuery.append(" Order by RECEIPT_DOC_NO ASC");
				
				log.info(CommonService.ReplaceStringToTxtDatabase(_strQuery.toString()));
				
				//List<Object[]> result1 = (List<Object[]>)EmSynchro.createNativeQuery(_strQuery.toString()).getResultList();		 
				List<String> result1 = (List<String>)getEm().createNativeQuery(_strQuery.toString()).getResultList();
				List<String> rr_lst= new ArrayList<String>(); 
				 if(!CommonService.IsNull(result1)){
					 
					 for (String resultElement : result1) {
						 
						 String RECEIPT_DOC_NO = CommonService.IsNullRString(resultElement);
						 
						 log.info("RECEIPT_DOC_NO : " + RECEIPT_DOC_NO);					 
						 if(!CommonService.IsNull(RECEIPT_DOC_NO))
							 rr_lst.add(RECEIPT_DOC_NO);
					 }
					 return rr_lst;
				 }
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		return null;
	}
	
	public FiArreceiptreOutb InsertedOrUpdated(FiArreceiptreOutb vlaue) throws Exception {
		FiArreceiptreOutb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("FI_ARRECEIPTRE_OUTB");					
				obj.setPromisStagingId(id);
						
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				
				if(CommonService.IsNull(obj.getUpdatedOn()))
					obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
				else
					obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
				
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<FiArreceiptreOutb> Inserted(List<FiArreceiptreOutb> vlauelist) throws Exception {
		log.info("Inserted ====> ");
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiArreceiptreOutb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("FI_ARRECEIPTRE_OUTB");					
					obj.setPromisStagingId(id);
							
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					
					if(CommonService.IsNull(obj.getUpdatedOn()))
						obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
					else
						obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
					
					log.info(obj.toString());
					common_lst.add(obj);
				}	
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<FiArreceiptreOutb> Updated(List<FiArreceiptreOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiArreceiptreOutb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
